package kr.hkit.androidcomplete03.ch20;

import android.R;
import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class WordListFragment extends ListFragment {
	OnWordChangListener mHost;
	
	public interface OnWordChangListener{
		public void OnWordChanged(int index);
	}
	
	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		
		try{
			mHost = (OnWordChangListener) activity;
		}catch (ClassCastException e){
			throw new ClassCastException("activity have to implement OnWordChanged");
		}
	}// end OnAttach
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		String WORD[] = getResources().getStringArray(kr.hkit.androidcomplete03.R.array.ch20_listfragment_word);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.simple_list_item_activated_1,
				WORD);
		setListAdapter(adapter);
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		l.setItemChecked(position, true);
		mHost.OnWordChanged(position);
	}
}
